package com.kerwin.flutter_android;

import static com.kerwin.flutter_android.MyApplication.CACHED_ENGINE_ID;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;

public class MainActivity extends AppCompatActivity {
    FlutterEngine flutterEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //一个Engine代表的时一个flutter的进程实例（dart解析器，flutter平台线程）
        flutterEngine = new FlutterEngine(this);
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );
    }

    public void test(View view) {
//        startActivity(
//                FlutterActivity
//                        //withNewEngine 回去创建一套flutter实例数据
////                        .withNewEngine()
//                        .withCachedEngine(CACHED_ENGINE_ID)
//                        .build(MainActivity.this)
//        );


//        FlutterFragment.withNewEngine().initialRoute("/").build();
//
//        getSupportFragmentManager().beginTransaction();
//
//        FlutterView flutterView = new FlutterView(this);
//        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        FrameLayout flContainer = findViewById(R.id.fl_flutter);
//        flContainer.addView(flutterView, lp);
//        // 关键代码，将Flutter页面显示到FlutterView中
//        flutterView.attachToFlutterEngine(flutterEngine);

        FlutterAppActivity.start(MainActivity.this,"1111");


    }
}